﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace GU.WebERP.Core.Utils
{
    /// <summary>
    /// 转换HashTable帮助类
    /// </summary>
    public class ConvertHashTable
    {
        /// <summary>
        /// 对象json数据源转换Hashtable键值对
        /// </summary>
        /// <param name="json"></param>
        public static Hashtable ToHashTable(string json)
        {
            Regex regex = new Regex(@"^\{.+\}$", RegexOptions.Singleline);
            if (!regex.IsMatch(json))
                throw new Exception("非对象的json格式{ }");

            DataTable dtSource = new DataTable();
            JObject jobj = JsonConvert.DeserializeObject<JObject>(json);
            var collection = jobj.AsJEnumerable();

            foreach (JProperty item in collection)
            {
                dtSource.Columns.Add(item.Name);
            }

            DataRow row = dtSource.NewRow();
            foreach (JProperty item in collection)
            {
                row[item.Name] = item.Value.ToString();
            }
            dtSource.Rows.Add(row);

            return ToHashTable(dtSource);
        }

        /// <summary>
        /// DataTable数据源转换Hashtable键值对
        /// </summary>
        /// <param name="dtData">数据源</param>
        public static Hashtable ToHashTable(DataTable dtData)
        {
            if (dtData == null)
                return null;

            Hashtable ht = new Hashtable();
            foreach (DataRow row in dtData.Rows)
            {
                foreach (DataColumn item in dtData.Columns)
                {
                    if (ht.ContainsKey(item.ColumnName))
                        throw new Exception("DataTable中存在重复的列 " + item.ColumnName);
                    if (row[item.ColumnName] == null)
                        continue;

                    ht.Add(item.ColumnName, row[item.ColumnName]);
                }
                break;
            }

            return ht;
        }

        /// <summary>
        /// json数据源转换Hashtable键值对的List
        /// </summary>
        /// <param name="dtData">数据源</param>
        /*public static List<Hashtable> ToHashTableList(string json)
        {
            Regex regex = new Regex(@"^[.+]$", RegexOptions.Singleline);
            if (!regex.IsMatch(json))
                throw new Exception("非对象的json格式{ }");

            List<Hashtable> htList = new List<Hashtable>();
            DataTable dtData = JsonConvert.DeserializeObject<DataTable>(json);
            return ToHashTableList(dtData);
        }*/

        /// <summary>
        /// DataTable数据源转换Hashtable键值对的List
        /// </summary>
        /// <param name="dtData">数据源</param>
        public static List<Hashtable> ToHashTableList(DataTable dtData)
        {
            if (dtData == null)
                return null;

            List<Hashtable> htList = new List<Hashtable>();
            foreach (DataRow row in dtData.Rows)
            {
                Hashtable ht = new Hashtable();
                foreach (DataColumn item in dtData.Columns)
                {
                    if (ht.ContainsKey(item.ColumnName))
                        throw new Exception("DataTable中存在重复的列 " + item.ColumnName);
                    if (row[item.ColumnName] == null)
                        continue;

                    ht.Add(item.ColumnName, row[item.ColumnName]);
                }

                htList.Add(ht);
            }
            return htList;
        }
    }
}
